package com.dalei.vds.soap;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;

import com.dalei.vdserver.shared.model.DetectionTask;
import com.dalei.vdserver.shared.model.ErrorCode;
import com.dalei.vdserver.shared.model.VehicleInformation;
import com.dalei.vdserver.shared.soap.DetectionSoap;
import com.google.gson.Gson;

public class DetectionSoapImpl extends BaseSoap implements DetectionSoap {

	private String url;

	private HttpTransportSE ht;

	private SoapSerializationEnvelope envelope;

	public DetectionSoapImpl(String ipAddress, int port, int timeout) {
		// TODO Auto-generated constructor stub
		super(ipAddress, port, timeout);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("http://");
		stringBuffer.append(super.getIpAddress());
		stringBuffer.append(":");
		stringBuffer.append(super.getPort());
		stringBuffer.append("/VDServer/DetectionService?wsdl");
		url = stringBuffer.toString();
		ht = new HttpTransportSE(url, super.getTimeout());
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	}

	public DetectionSoapImpl(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("http://");
		stringBuffer.append(super.getIpAddress());
		stringBuffer.append(":");
		stringBuffer.append(super.getPort());
		stringBuffer.append("/VDServer/DetectionService?wsdl");
		url = stringBuffer.toString();
		ht = new HttpTransportSE(url, super.getTimeout());
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	}

	@SuppressWarnings("unchecked")
	public List<DetectionTask> getDetectionTasks(int deviceId) {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "getDetectionTasks");
		envelope.bodyOut = request;
		request.addProperty("deviceId", deviceId);
		
		List<DetectionTask> tasks = new ArrayList<DetectionTask>();
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String json = result.getProperty("return").toString();
			Gson gson = new Gson();
			List<Map<String, String>> mapList = gson.fromJson(json, List.class);
			for (Map<String, String> map : mapList) {
				DetectionTask task = new DetectionTask();

				task.setFid((int) Double.parseDouble(map.get("fid")));

				String licensePlateNumber = map.get("licensePlateNumber");
				if (licensePlateNumber != null) {
					task.setLicensePlateNumber(licensePlateNumber);
				} else {
					task.setLicensePlateNumber(null);
				}

				String timestamp = map.get("timestamp");
				DateFormat dateFormat = new SimpleDateFormat(
						"MMM dd, yyyy hh:mm:ss aa", Locale.ENGLISH);
				try {
					Date date = dateFormat.parse(timestamp);
					task.setTimestamp(date);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					task.setTimestamp(null);
				}

				tasks.add(task);
			}
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return tasks;
	}

	public int lockDetectionTask(int fid) {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "lockDetectionTask");
		envelope.bodyOut = request;
		request.addProperty("fid", fid);
		int i = 0;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String returnObject = result.getProperty("return").toString();
			i = Integer.parseInt(returnObject);
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		}
		return i;
	}
	
	public int unlockDetectionTask(int fid) {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "unlockDetectionTask");
		envelope.bodyOut = request;
		request.addProperty("fid", fid);
		int i = 0;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String returnObject = result.getProperty("return").toString();
			i = Integer.parseInt(returnObject);
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		}
		return i;
	}
	
	public VehicleInformation getVehicleInformation(int fid) {
		SoapObject request = new SoapObject(NAMESPACE, "getVehicleInformation");
		envelope.bodyOut = request;
		request.addProperty("fid", fid);

		VehicleInformation information;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String json = result.getProperty("return").toString();
			Gson gson = new Gson();
			information = gson.fromJson(json, VehicleInformation.class);
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return information;
	}

	public int saveDetectionResult(String json, long crc32) {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "saveDetectionResult");
		envelope.bodyOut = request;
		request.addProperty("json", json);
		request.addProperty("crc32", crc32);
		int i = 0;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String returnObject = result.getProperty("return").toString();
			i = Integer.parseInt(returnObject);
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		}
		return i;
	}
}
